package com.cloudfun.campusshare.configuration.jpa.util;

import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 王溪源
 * @ClassName: MatchValue
 * @Description: 对比值实体，根据该类的信息进行对一个或者多个属性值，使用or还是and 进行条件关联
 * @Date 2020/8/10 11:06
 **/
public class MatchValue {
    private boolean hasOrOperate;

    private List<Object> values;

    public MatchValue(boolean hasOrOperate, List<Object> values) {
        this.hasOrOperate = hasOrOperate;
        this.values = values;
    }

    public boolean isHasOrOperate() {
        return hasOrOperate;
    }

    public List<Object> getValues() {
        return values;
    }

    public static MatchValue createMatchValueModel(String matchValue, Class<?> type, String andValueSeparator, String orValueSeparator) {
        List<Object> values = new ArrayList<> ();
        if (StringUtils.contains (matchValue, andValueSeparator)) {
            String[] spiltValues = StringUtils.splitByWholeSeparator (matchValue, andValueSeparator);
            CollectionUtils.mergeArrayIntoCollection (ConvertUtils.convert (spiltValues, type), values);
            return new MatchValue (false, values);
        } else if (StringUtils.contains (matchValue, orValueSeparator)) {
            String[] spiltValues = StringUtils.splitByWholeSeparator (matchValue, orValueSeparator);
            CollectionUtils.mergeArrayIntoCollection (ConvertUtils.convert (spiltValues, type), values);
            return new MatchValue (true, values);
        } else {
            values.add (ConvertUtils.convert (matchValue, type));
            return new MatchValue (false, values);
        }
    }
}
